home *** CD-ROM | disk | FTP | other *** search
- /* machine_amiga.c (c) 1999 by Volker Remuß, remuss@cs.tu-berlin.de */
-
- #include "machine_amiga.h"
- #include "frontend.h"
-
-
- #include <exec/types.h>
- #include <exec/io.h>
- #include <exec/exec.h>
- #include <devices/serial.h>
- #include <dos.h>
-
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <clib/exec_protos.h>
-
- #include <stdio.h>
- #include <stdlib.h>
-
-
- struct MsgPort *SerialMP=0;
- struct IOExtSer *SerialIO=0;
-
- int seropen=0;
-
- void seriofailed(char *string, int32 res)
- {
- switch (res)
- {
- case -1: printf("Serial IO failed: %s : Unkown Device\n", string); break;
- case 1: printf("Serial IO failed: %s : Device is busy\n", string); break;
- default: printf("Serial IO failed: %s : %d\n", string, res); break;
- }
- //myexit(10);
- }
-
- void closeserial(void)
- {
- if (seropen) { CloseDevice((struct IORequest *)SerialIO); seropen = 0; }
- if (SerialMP) { DeletePort(SerialMP); SerialMP = 0; }
- if (SerialIO) { DeleteExtIO((struct IORequest *)SerialIO); SerialIO = 0; }
- }
-
-
- int initserial(char *device, int unit)
- {
- int res=-25;
-
- if ( (!(SerialMP=CreatePort(0, 0)))
- || (!(SerialIO=(struct IOExtSer *)CreateExtIO(SerialMP, sizeof(struct IOExtSer))))
- || (res=OpenDevice(device, unit, (struct IORequest *)SerialIO, 0)))
- {
- seriofailed("Initserial failed!", res);
- myexit(10);
- }
- seropen = 1;
-
- SerialIO->IOSer.io_Command = SDCMD_SETPARAMS;
- SerialIO->io_SerFlags = SERF_XDISABLED;
- SerialIO->io_RBufLen = 64*36;
- SerialIO->io_Baud = 19200;
- SerialIO->io_ReadLen = 8;
- SerialIO->io_WriteLen = 8;
- SerialIO->io_StopBits = 1;
-
- if (res = DoIO((struct IORequest *)SerialIO))
- {
- seriofailed("Error in setting serialparams", res);
- myexit(10);
- }
- return (0);
- }
-
- int setlocalserialspeed(int speed)
- {
- int res;
-
- SerialIO->IOSer.io_Command = SDCMD_SETPARAMS;
- SerialIO->io_Baud = speed;
-
- if (res = DoIO((struct IORequest *)SerialIO))
- {
- seriofailed("Unable to set new host speed.", res);
- myexit(10);
- }
- return (0);
- }
-
-
- int16 inreadbuffer(void)
- {
- int newchars;
- int res;
-
- SerialIO->IOSer.io_Command = SDCMD_QUERY;
- if (res=DoIO((struct IORequest *)SerialIO))
- {
- seriofailed("inreadbuffer", res);
- return(0);
- }
-
- newchars = SerialIO->IOSer.io_Actual;
- return((int16)newchars);
- }
-
-
- int16 readserial(unsigned char *destbuf, int16 buflength, int16 howmuch)
- {
- int res;
- int newchars;
-
- if (howmuch)
- {
- SerialIO->IOSer.io_Command = CMD_READ;
- SerialIO->IOSer.io_Length = howmuch;
- SerialIO->IOSer.io_Data = destbuf;
- if (res=DoIO((struct IORequest *)SerialIO))
- {
- //seriofailed("readserial 1", res);
- delay(1);
- while (readserial(destbuf, buflength, 0)) ;//printf("Reread @ 1\n");
- }
- return(howmuch);
- }
- else
- {
- if ((newchars = inreadbuffer()) > buflength)
- newchars = buflength;
-
- SerialIO->IOSer.io_Command = CMD_READ;
- SerialIO->IOSer.io_Length = newchars;
- SerialIO->IOSer.io_Data = destbuf;
- if (res=DoIO((struct IORequest *)SerialIO))
- {
- //seriofailed("readserial 2", res);
- delay(1);
- while (readserial(destbuf, buflength, 0)) ; //printf("Reread @ 2\n");
- newchars=0;
- }
-
- return((int16)newchars);
- }
- }
-
-
- int16 writeserial(unsigned char *databuf, int16 datalength)
- {
- int res;
-
- SerialIO->IOSer.io_Command = CMD_WRITE;
- SerialIO->IOSer.io_Length = datalength;
- SerialIO->IOSer.io_Data = databuf;
-
- if (res=DoIO((struct IORequest *)SerialIO))
- {
- seriofailed("Writing", res);
- myexit(10);
- }
-
- return(0);
- }
-
- void delay(int16 ticks)
- {
- Delay(ticks); // tick = 1/50 sek. on Amiga
- }
-